package net.gdface.jna;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.FloatByReference;

//import static net.gdface.jna.DataFrequentUtil.getDataFrequent;

public class Main {

//    public void fftCalculator(int channel_num, int t_paraNum, int f_freq_N, int f_peak_N, String device_name, float freq_nature, float freq_sample, FloatByReference N_freq_para,
//                              FloatByReference sensor_data, )

    public static void main(String[] args) throws Exception {

        FloatByReference fbr = new FloatByReference();
        Pointer p = Pointer.createConstant(10);

        fbr.setPointer(p);


        List<String> lines = FileUtil.loadData("D:\\omen_data_4.txt");
        //List<String> lines = FileUtil.loadData("omen_data_4.txt");

        List<Float> floatsList = convertLineToData(lines);

        int channel = 2; //通道数
        int size_x = 16384;//通道数据长度
        int t_paranum = 5;//时域参数计算结果长度
        int f_freq_N = 14;//倍频数
        int f_peak_N = 10;////峰值数0

        int freq_domain_N = 8;

        OmenData omenDate = new OmenData();
        omenDate.setChannel_num(channel);
        omenDate.setData_len(size_x);

        omenDate.setFft_len((int) (size_x/2+1));
        omenDate.setDevice_name_len(5);

        omenDate.setFreq_peak_N(10);
        omenDate.setTime_para_N(5);

        omenDate.setFreq_domain_N(freq_domain_N);

        String device_name = "testxin";
        Pointer p_device_name = new Memory(device_name.length() + 1); // WARNING: assumes ascii-only string
        p_device_name.setString(0, device_name);
        omenDate.setDevice_name(p_device_name);


        omenDate.setFreq_natural(1490f/60);
        omenDate.setFreq_sample(21700f);
        //N倍频值个数，14
        omenDate.setFreq_freq_N(f_freq_N);
        //倍频数，暂定｛0.2,0.25,0.33,0.5,1,2,3,4,5,6,7,8,9,10｝
        FloatByReference p_n_freq_para = new FloatByReference();
        float[] ppints={0.2f,0.25f,0.33f,0.5f,1,2,3,4,5,6,7,8,9,10};
        Memory pp =new Memory(100);
        pp.write(0, ppints, 0, ppints.length);
        p_n_freq_para.setPointer(pp);
        omenDate.setN_freq_para(p_n_freq_para);

        //float* N_domain_para;//区域频值和，现在是四个区域：0-0.3x，0.3-0.5x，3-5x，5-10x
        FloatByReference p_n_domain_para = new FloatByReference();
        float[] ppdomains = {0,0.3f,0.3f,0.5f,3,5,5,10};
        Memory pp1 = new Memory(100);
        pp1.write(0,ppdomains,0,ppdomains.length);
        p_n_domain_para.setPointer(pp1);
        omenDate.setN_domain_para(p_n_domain_para);

        FloatByReference fft_data = new FloatByReference();
        Pointer pp_fft_data = new Memory(1000000);
        int  fft_data_arr_len = channel * ((int) (size_x/2+1));
        float[] fft_data_arr = new float[fft_data_arr_len];
        pp_fft_data.write(0, fft_data_arr, 0, fft_data_arr.length);

        fft_data.setPointer(pp_fft_data);

        FloatByReference fft_f = new FloatByReference();
        Pointer pp_fft_f = new Memory(1000000);
        int fft_f_arr_len = channel * ((int) (size_x/2+1));
        float[] fft_f_arr = new float[fft_f_arr_len];
        pp_fft_f.write(0, fft_f_arr, 0, fft_f_arr.length);
        fft_f.setPointer(pp_fft_f);

        FloatByReference t_para = new FloatByReference();
        Pointer pp_t_para = new Memory(1000000);
        int t_para_arr_len = t_paranum * channel;
        float[] t_para_arr = new float[t_para_arr_len];
        pp_t_para.write(0, t_para_arr, 0, t_para_arr.length);
        t_para.setPointer(pp_t_para);

//        FloatByReference n_freq = new FloatByReference();
//        Pointer pp_n_freq = new Memory(1000000);
//        int n_freq_arr_len = f_freq_N * channel;
//        float[] n_freq_arr = new float[n_freq_arr_len];
//        pp_n_freq.write(0, n_freq_arr, 0, n_freq_arr.length);
//        n_freq.setPointer(pp_n_freq);

        //new same as above
        FloatByReference n_freq_val = new FloatByReference();
        Pointer pp_n_freq_val = new Memory(1000000);
        int n_freq_arr_len = f_freq_N * channel;
        float[] n_freq_arr = new float[n_freq_arr_len];
        pp_n_freq_val.write(0, n_freq_arr, 0, n_freq_arr.length);
        n_freq_val.setPointer(pp_n_freq_val);

        //new
        FloatByReference n_freq_frq = new FloatByReference();
        Pointer pp_n_freq_frq = new Memory(1000000);
        int n_freq_frq_arr_len = channel * f_freq_N;
        float[] n_freq_frq_arr = new float[n_freq_frq_arr_len];
        pp_n_freq_frq.write(0,n_freq_frq_arr,0, n_freq_frq_arr.length);
        n_freq_frq.setPointer(pp_n_freq_frq);

        //new
        FloatByReference domain_freq_frq = new FloatByReference();
        Pointer pp_domain_freq_frq = new Memory(1000000);
        int domain_freq_frq_arr_len = channel*((int)(freq_domain_N/2));
        float[] domain_freq_frq_arr = new float[domain_freq_frq_arr_len];
        pp_domain_freq_frq.write(0, domain_freq_frq_arr,0, domain_freq_frq_arr.length);
        domain_freq_frq.setPointer(pp_domain_freq_frq);

//        FloatByReference peak_freq_val = new FloatByReference();
//        Pointer pp_peak_freq_val = new Memory(1000000);
//        int peak_freq_val_arr_len = channel * f_peak_N;
//        float[] peak_freq_val_arr = new float[peak_freq_val_arr_len];
//        pp_peak_freq_val.write(0, peak_freq_val_arr, 0, peak_freq_val_arr.length);
//        peak_freq_val.setPointer(pp_peak_freq_val);
        FloatByReference peak_freq_val = new FloatByReference();
        Pointer pp_peak_freq_val = new Memory(1000000);
        int peak_freq_val_arr_len = channel * f_peak_N;
        float[] peak_freq_val_arr = new float[peak_freq_val_arr_len];
        pp_peak_freq_val.write(0, peak_freq_val_arr, 0, peak_freq_val_arr.length);
        peak_freq_val.setPointer(pp_peak_freq_val);

        FloatByReference peak_freq_frq = new FloatByReference();
        Pointer pp_peak_freq_frq = new Memory(1000000);
        int peak_freq_frq_arr_len = channel * f_peak_N;
        float[] peak_freq_frq_arr = new float[peak_freq_frq_arr_len];
        pp_peak_freq_frq.write(0, peak_freq_frq_arr, 0, peak_freq_frq_arr.length);
        peak_freq_frq.setPointer(pp_peak_freq_frq);

//        FloatByReference peak_freq_frq = new FloatByReference();
//        Pointer pp_peak_freq_frq = new Memory(1000000);
//        int peak_freq_frq_arr_len = channel * f_peak_N;
//        float[] peak_freq_frq_arr = new float[peak_freq_frq_arr_len];
//        pp_peak_freq_frq.write(0, peak_freq_frq_arr, 0, peak_freq_frq_arr.length);
//        peak_freq_frq.setPointer(pp_peak_freq_frq);

        FloatByReference sensor_data = new FloatByReference();
        Pointer pp_sensor_data = new Memory(10000000);
        int pp_sensor_data_arr_len = 10;
//        float[] sensor_data_arr = new float[pp_sensor_data_arr_len];
//        pp_sensor_data.write(0, sensor_data_arr, 0, sensor_data_arr.length);
        sensor_data.setPointer(pp_sensor_data);

        omenDate.setFft_data(fft_data);
        omenDate.setFft_f(fft_f);
        omenDate.setT_para(t_para);
        omenDate.setN_freq_val(n_freq_val);
        omenDate.setPeak_freq_val(peak_freq_val);
        omenDate.setPeak_freq_frq(peak_freq_frq);
        omenDate.setDomain_freq_frq(domain_freq_frq);
        omenDate.setN_freq_frq(n_freq_frq);






        if (floatsList !=null && floatsList.size()>0) {
            float[] floats = new float[floatsList.size()];

            for (int i=0;i<floatsList.size();i++) {
                float float1 = floatsList.get(i);
                floats[i]=float1;
//                System.out.println(floats[i]);
            }
            pp_sensor_data.write(0, floats, 0, floats.length);
            sensor_data.setPointer(pp_sensor_data);
            omenDate.setSensor_data(sensor_data);
            FftdllLibrary.INSTANCE.omen_function(omenDate);
            //right
            Pointer rp_fft_date = omenDate.getFft_data().getPointer();
            float[] rp_fft_date_arr = rp_fft_date.getFloatArray(0, fft_data_arr_len);

            omenDate.setPeak_freq_frq(peak_freq_frq);

            Pointer rp_fft_f = omenDate.getFft_f().getPointer();
            float[] rp_fft_f_arr = rp_fft_f.getFloatArray(0, fft_f_arr_len);


            Pointer rp_t_para = omenDate.getT_para().getPointer();
            float[] rp_t_para_arr = rp_t_para.getFloatArray(0, t_para_arr_len);
            //wrong
            Pointer rp_n_freq_val = omenDate.getN_freq_val().getPointer();
            float[] rp_n_freq_arr = rp_n_freq_val.getFloatArray(0, n_freq_arr_len);

            Pointer rp_domain_freq_frq = omenDate.getDomain_freq_frq().getPointer();
            float[] rp_domain_freq_frq_arr = rp_domain_freq_frq.getFloatArray(0,domain_freq_frq_arr_len);

            Pointer rp_n_freq_frq = omenDate.getN_freq_frq().getPointer();
            float[] rp_n_freq_frq_arr = rp_n_freq_frq.getFloatArray(0,n_freq_frq_arr_len);

            //wrong
            Pointer rp_peak_freq_val = omenDate.getPeak_freq_val().getPointer();
            float[] rp_peak_freq_val_arr = rp_peak_freq_val.getFloatArray(0, peak_freq_val_arr_len);
            //wrong
            Pointer rp_peak_freq_frq = omenDate.getPeak_freq_frq().getPointer();
            float[] rp_peak_freq_frq_arr = rp_peak_freq_frq.getFloatArray(0, peak_freq_frq_arr_len);

//            Map<String, float[][]> resultMap = getDataFrequent(rp_fft_date_arr, rp_fft_f_arr, new float[]{0.0F, 9.0F, 9.0F, 15F, 90F, 150F, 150F, 300F}, 4, 10);
//            System.out.println(Arrays.deepToString(resultMap.get(DataFrequentUtil.DATA_KEY_GROUP_DATA)));
//            System.out.println(Arrays.deepToString(resultMap.get(DataFrequentUtil.DATA_KEY_PEAK_DATA)));
//            System.out.println(Arrays.deepToString(resultMap.get(DataFrequentUtil.DATA_KEY_PEAK_FREQUENT)));
//            System.out.println("**********************");
//            System.out.println("channel_num:"+omenDate.getChannel_num()+
//                    " freq_peak_N:"+f_peak_N+
//                    " time_para_N:"+omenDate.getTime_para_N()+
//                    " data_len:"+omenDate.getData_len()+
//                    " fft_len:"+omenDate.getFft_len()
//            );
//            System.out.println("**********************");
//            System.out.println("device_name:"+omenDate.getDevice_name()+
//                    " device_name_len:"+omenDate.getDevice_name_len()+
//                    " freq_natural:"+omenDate.getFreq_natural()+
//                    " freq_sample:"+omenDate.getFreq_sample()+
//                    " freq_freq_N:"+omenDate.getFreq_freq_N()
//            );
//
//            System.out.println("**********************"+" 倍频数 "+"**********************");
//            StringBuffer nFreqParaSb = new StringBuffer();
//            for (int i = 0;i<ppints.length;i++){
//                nFreqParaSb.append(ppints[i]+" ");
//            }
//            System.out.println(nFreqParaSb);
//
//            System.out.println("**********************"+" 区域频值和 "+"**********************");
//            StringBuffer ppDomainSb = new StringBuffer();
//            for (int i = 0;i<ppdomains.length;i++){
//                ppDomainSb.append(ppdomains[i]+" ");
//            }
//            System.out.println(ppDomainSb);
//
//            System.out.println("**********************"+" 传感器数据 "+"**********************");
//            StringBuffer sensorDataSb = new StringBuffer();
//            for (int i = 0;i<ppdomains.length;i++){
//                sensorDataSb.append(ppdomains[i]+" ");
//            }
//            System.out.println(ppDomainSb);
//
            StringBuffer sb = new StringBuffer();
            for(int i = 0; i<rp_domain_freq_frq_arr.length;i++){
                sb.append(rp_domain_freq_frq_arr[i]+" ");

            }

            System.out.println(sb);
            System.out.println(omenDate.toString()+"^^^^^^^^^^^^^^^^^^^^^^^^^^");
            System.out.println(omenDate.getFft_len()+"fft_len");
//
//
//
//
//
//
//
//           /*  for (float f : fft_date_array) {
//            	 BigDecimal decimal = new BigDecimal(f);
//            	 BigDecimal setScale = decimal.setScale(8,BigDecimal.ROUND_HALF_DOWN);
//            	 System.out.println("fft_date:"+setScale);
//
//			}*/
        }



        //  FileUtil.saveData("D:\\test.txt", Arrays.asList(complexes));

    }


    private static  List<Float> convertLineToData(List<String> lines){
        // List<Complex> complexList = new ArrayList<Complex>();
        List<Float> floatsList = new ArrayList<Float>();
        for(int i=0 ; i<lines.size();i++){
            String line = lines.get(i);
            if(StringUtils.isNotBlank(line)){
                String[] arr = line.split(" ");
                if(arr!=null&&arr.length > 0){
                    for (int j = 0; j < arr.length; j++) {
                        floatsList.add(Float.valueOf(arr[j]));
                    }
                }
            }
        }
        return floatsList;
    }
}
